<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('渠道代理商列表')"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label style="width: 100px;">代理商编号：</label>
                            <input type="text" name="agentId"/>
                        </li>

                        <li>
                            <label style="width: 100px;">状态：</label>
                            <select name="status" th:with="type=${@dict.getType('ls_channel_agent_status')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <label style="width: 10px;"></label>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="add()" shiro:hasPermission="channel:agent:add">
                <i class="fa fa-plus"></i> 添加
            </a>
            <a class="btn btn-primary single disabled" onclick="edit()" shiro:hasPermission="channel:agent:edit">
                <i class="fa fa-edit"></i> 修改
            </a>
            <a class="btn btn-primary multiple disabled" onclick="query()">
                <i  class="fa fa-search"></i> 查询结果
            </a>

            <a class="btn btn-success single disabled" onclick="rateConfig()" shiro:hasPermission="channel:agent:edit">
                <i class="fa fa-plus"></i> 成本配置
            </a>
            <a class="btn btn-primary single disabled" onclick="rateQuery()" shiro:hasPermission="channel:agent:edit">
                <i class="fa fa-edit"></i> 查看成本
            </a>

            <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()"
               shiro:hasPermission="channel:agent:remove">
                <i class="fa fa-remove"></i> 删除
            </a>
            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="channel:agent:export">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table" class="table text-nowrap"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('channel:agent:edit')}]];
    var removeFlag = [[${@permission.hasPermi('channel:agent:remove')}]];
    var statusDatas = [[${@dict.getType('ls_channel_agent_status')}]];
    var prefix = ctx + "channel/agent";
    var channelType = [[${@dict.getType('channel_type')}]];
    $(function () {
        var options = {
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "渠道代理商",
            firstLoad: false,
            clickToSelect: true,
            columns: [{
                checkbox: true
            },
                {
                    field: 'id',
                    title: '主键ID',
                    visible: false
                },
                {
                    field: 'channelCode',
                    title: '渠道',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(channelType, value);
                    }
                },
                {
                    field: 'agentId',
                    title: '代理商编号'
                },
                {
                    field: 'cagentId',
                    title: '渠道代理商编号'
                },
                {
                    field: 'enterpriseTitle',
                    title: '公司名称'
                },
                {
                    field: 'legalPersonName',
                    title: '法人姓名'
                },
                {
                    field: 'feeInfo',
                    title: '渠道成本',
                    formatter: function(value, row, index) {
                        try {
                            const parsed = JSON.parse(value);
                            if (Array.isArray(parsed) && parsed.length > 0) {
                                return '<span style="color: green;">已配置</span>';
                            } else {
                                return '<span style="color: gray;">未配置</span>';
                            }
                        } catch (e) {
                            // 如果不是有效的 JSON，也视为未配置
                            return '<span style="color: gray;">未配置</span>';
                        }
                    }
                },
                {
                    field: 'status',
                    title: '当前处理状态',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(statusDatas, value);
                    }
                },
                {
                    field: 'drawPattern',
                    title: '分润提取模式',
                    formatter: function (value) {
                        switch (value) {
                            case '1': return '按天（代发）';
                            case '2': return '按月（不代发）';
                            default: return '-';
                        }
                    }
                },
                {
                    field: 'profitRuleType',
                    title: '分成模式',
                    formatter: function (value) {
                        switch (value) {
                            case '0': return '成本规则';
                            case '1': return 'SaaS 高签';
                            case '2': return '成本比例高签';
                            case '3': return '刷宝3.0';
                            default: return '-';
                        }
                    }
                },
                {
                    field: 'lowerDivideCent',
                    title: '分成比例',
                    formatter: function (value) {
                        return value != null ? value + '%' : '-';
                    }
                },
                {
                    field: 'subProfitBillFlag',
                    title: '是否独立结算',
                    formatter: function (value) {
                        switch (value) {
                            case '0': return '否';
                            case '1': return '是';
                            default: return '-';
                        }
                    }
                },
                {
                    field: 'channelResponse',
                    title: '渠道返回信息'
                },
                {
                    field: 'remark',
                    title: '备注信息'
                },
                ]
        };
        $.table.init(options);
    });

    function add() {
        $.modal.open("添加服务商", prefix + "/add", 1200, 600);
    }

    function rateConfig() {

        var agentId = $.table.selectColumns("agentId");
        var agentName = $.table.selectColumns("enterpriseTitle");
        var feeInfo =$.table.selectColumns("feeInfo");
        var title = (Array.isArray(feeInfo) && feeInfo.length > 0)
            ? "修改服务商成本（" + agentId + " - " + agentName + "）"
            : "添加服务商成本（" + agentId + " - " + agentName + "）";

        $.modal.openFull(title, prefix + "/addRate/" + agentId);

    }

    function edit() {
        var id = $.table.selectColumns("id");
        $.modal.open("修改服务商", prefix + "/edit/" + id, "1200", "600");
    }
    function query() {
        var rows = $.table.selectFirstColumns(); // 获取选中的行的主键 id（默认选中列）
        if (rows.length == 0) {
            $.modal.alertWarning("请至少选择一条记录");
            return;
        }
        $.modal.confirm("确认要执行渠道代理查询操作吗？", function () {
            $.operate.post(ctx + "channel/agent/query", { ids: rows.join() });
        });
        // $.modal.confirm("确认要执行渠道代理查询操作吗？", function () {
        //     $.ajax({
        //         type: "post",
        //         url: ctx + "channel/agent/query",  // 替换为你的实际路径
        //         data: { ids: rows.join() },        // 将数组转成逗号分隔字符串
        //         success: function (result) {
        //             $.operate.successCallback(result);
        //         }
        //     });
        // });
    }
    function rateQuery() {
        var agentId = $.table.selectColumns("agentId");
        var agentName = $.table.selectColumns("enterpriseTitle");
        var title = "查看服务商成本（" + agentId + " - " + agentName + "）";
        $.modal.openFull(title, prefix + "/queryRate/" + agentId);
    }
</script>
</body>
</html>